$(function(){
	initBlockUI();
	getCompanyListViaJs("#com_select", -1);
	getSectionListViaJs("#sec_select", -1, -1);
	$("#com_select").change(function(){
		getSectionListViaJs("#sec_select", $(this).val(), -1);
	});
   $("#sec_select").change(function(){
	  var com_name = $("#com_select")
		.find("option[@value='" + $("#com_select").val() + "']")
		  .html();
	  var sec_name = $(this)
		.find("option[@value='" + $(this).val() + "']")
		  .html();
	  $("#pos_name").html(com_name + " -&gt; " + sec_name);
	  showPositions($(this).val());
   });
});

// 更新数据表格
function showPositions(section_id)
{
   var tbody = $("#tbody2");
   var url = "position_power.php";
   var pars = { "page_status": 1, "section_id": section_id };
   
   $.post(url, pars, function(xmlobj){
	  tbody.html("");
	  $(xmlobj).find("item").each(function(i){
		 var item_id = $(this).find("id").text();
		 var item_zhname = $(this).find("zhname").text();
		 var item_power = $(this).find("power").text();
		 tbody.append($("<tr></tr>")
		 			.append($("<th></th>").html(i + 1))
					  .append($("<td></td>").html(item_zhname))
					  .append($("<td></td>")
							  .html($("<a href=\"#\"></a>")
							  		.attr("id", "view_" + item_id)
							  		.attr("pid", item_id)
							  		.attr("power", item_power)
							  		.html("查看")
							  		.click(function() {
							  			showPower($(this).attr("pid"), $(this).attr("power"));
							  			return false;
							  		})
							  ))
					  .append($("<td></td>")
							  .html($("<a href=\"#\"></a>")
							  		.attr("id", "modify_" + item_id)
							  		.attr("pid", item_id)
							  		.attr("power", item_power)
							  		.html("修改")
							  		.click(function() {
							  			modifyPower($(this).attr("pid"), $(this).attr("power"));
							  			return false;
							  		})
							  )));
	  });
	  tablePretty("#tbody2");
   });
}

// 显示权限
function showPower(sec_id, power)
{
   var power_array = arrayUnique(power.split(",")).sort();
   var power_list = $("<div></div>").addClass("power_list")
	 .append($("<div></div>").addClass("modal_header").html("现有权限"))
	 .append($("<div></div>").addClass("modal_content").html(getPowerListViaJs(power_array)));
   $.modal(power_list, {
	  onShow: function() { m(1); },
	  onClose: function() { m(0); }
   });
}

// 修改权限
function modifyPower(pid, power)
{
	var t = [];
	$.each(__powerArray__, function(i, n) {
		t[t.length] = n[3];
	});
	var s = arrayUnique(power.split(",")).sort();
	var power_block = $("<div></div>")
		.append($("<div></div>").addClass("modal_header").html("修改权限"))
		.append($("<div></div>").addClass("modal_content").html(makePowerCheckBox(t, s, "power_check", "pc[]")))
		.append($("<div></div>").addClass("modal_footer").html($("<input type=\"button\" />")
			.val("提交")
			.click(function(){
				var power = [];
				$("#power_check input:checkbox[@name='pc\[\]']:checked").each(function(i) {
					power[power.length] = $(this).val();
				});
				var url = "position_power.php";
				var pars = { page_status: 2, pid: pid, power: power.join(",") };
				$.post(url, pars, function(text) {
					if (/^Success/.test(text)) {
						$("#view_" + pid + ", #modify_" + pid).attr("power", pars["power"]);
						m(0);
						$.modal.close();
					}
				});
			})
		));
	$.modal(power_block, {
		onShow: function() { m(1); },
		onClose: function() { m(0); }
	});
}

// modal 的关/启 
function m(flag) { modalShowClose(".left select", flag); }
